﻿Public Class frmLoaiNhanVien


    Private Sub frmLoaiNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDataKV()
        LockTextboxKV(False)
        cmbMaKV.Enabled = False
        LockButtonKV(True)
    End Sub

#Region "Xử lý load dữ liệu nhân viên"
    Private kvBUS As LoaiNhanVienBUS
    Private _emp As LoaiNhanVienDTO
    Private cm As CurrencyManager
    Private flagIsUp As Boolean

    Private Sub LoadDataKV()
        gvLoaiNhanVien.ClearSelection()
        kvBUS = New LoaiNhanVienBUS()
        Dim empList As New List(Of LoaiNhanVienDTO)
        empList = kvBUS.Getdata()
        cm = Me.BindingContext(empList)
        ClearBinding()
        ClearTextbox()
        cmbMaKV.DataBindings.Add("Text", empList, "Mã_loại_NV")
        txtTenKV.DataBindings.Add("Text", empList, "Tên_loại_NV")

        gvLoaiNhanVien.DataSource = empList
    End Sub

    Private Sub ClearBinding()
        cmbMaKV.DataBindings.Clear()
        txtTenKV.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxKV(ByVal f As Boolean)
        txtTenKV.Enabled = f
        If f = False Then
            txtTenKV.BackColor = Color.LemonChiffon
        Else
            txtTenKV.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextbox()
        cmbMaKV.Text = ""
        txtTenKV.Text = ""
    End Sub

    Private Sub LockButtonKV(ByVal dt As Boolean)
        btnThem.Enabled = dt
        btnSua.Enabled = dt
        btnXoa.Enabled = dt
        btnLuu.Enabled = Not dt
        btnHuy.Enabled = Not dt
    End Sub

    Private Sub btnKVSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        flagIsUp = False
        LockTextboxKV(True)
        LockButtonKV(False)
        gvLoaiNhanVien.Enabled = False
        txtTenKV.Focus()
    End Sub

    Private Sub btnKVHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        ClearBinding()
        LoadDataKV()
        LockTextboxKV(False)
        LockButtonKV(True)
        gvLoaiNhanVien.Enabled = True
    End Sub

    Private Sub btnKVThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        flagIsUp = True
        LockTextboxKV(True)
        LockButtonKV(False)
        ClearTextbox()
        gvLoaiNhanVien.Enabled = False
        txtTenKV.Focus()
    End Sub

    Private Sub btnKVLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim MaKV As String = cmbMaKV.Text
        Dim TenKV As String = txtTenKV.Text
        '-------------------------------------'
        If TestInfo() Then
            _emp = New LoaiNhanVienDTO(MaKV, TenKV)
            kvBUS = New LoaiNhanVienBUS()
            kvBUS.IsUpLNV(_emp, flagIsUp)
            ClearBinding()
            LoadDataKV()
            LockTextboxKV(False)
            LockButtonKV(True)
            gvLoaiNhanVien.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Function TestInfo() As Boolean
        TestInfo = True
        If Trim(txtTenKV.Text) = "" Then
            TestInfo = False
            MsgBox("Têm khu vực không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenKV.Focus()
            Exit Function
        End If
        Exit Function

    End Function

    Private Sub btnKVXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim MaKV As String = cmbMaKV.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Ma khu vực: " & cmbMaKV.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtTenKV.Text & " " & "", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _emp = New LoaiNhanVienDTO(MaKV)
            kvBUS = New LoaiNhanVienBUS()
            kvBUS.DeleteLNV(_emp)
            ClearBinding()
            LoadDataKV()
            LockTextboxKV(False)
            LockButtonKV(True)
            gvLoaiNhanVien.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub

#End Region

End Class